package com.xing.async;

import java.util.TimerTask;



/**
 * 系统内部的调度消息
 * 
 * 
 * 
 */
public abstract class ScheduledMessage implements ISenderMessage<InternalSender> {
	public static final int MESSAGE_STATE_INITIALIZED = 0; // 消息初始化
	public static final int MESSAGE_STATE_WAITING = 1; // 消息处于等待状态，可以被取消
	public static final int MESSAGE_STATE_INQUEUE = 2; // 执行中，已经投递到消息队列中了
	public static final int MESSAGE_STATE_CANCELED = 3; // 被取消
	public static final int MESSAGE_STATE_EXECUTING = 4; // 执行中，从队列中取出了
	public static final int MESSAGE_STATE_DONE = 5; // 执行完毕

	private final static InternalSender sender = new InternalSender();
	private TimerTask future;
	private int state;
	private final long createTimestamp;
	private long trigerTimestamp;

	@Override
	public int getInitBufferLength() {
		return 0;
	}

	@Override
	public int getLength() {
		return 0;
	}

	public ScheduledMessage(long createTime) {
		setState(MESSAGE_STATE_INITIALIZED);
		this.createTimestamp = createTime;
	}

	public long getTrigerTimestamp() {
		return trigerTimestamp;
	}

	public void setTrigerTimestamp(long finishTimestamp) {
		this.trigerTimestamp = finishTimestamp;
	}

	public synchronized int getState() {
		return state;
	}

	public synchronized boolean isCanceled() {
		return state == MESSAGE_STATE_CANCELED;
	}

	public synchronized void setState(int state) {
		this.state = state;
	}

	public TimerTask getFuture() {
		return future;
	}

	public void setFuture(TimerTask future) {
		this.future = future;
	}

	public synchronized void cancel() {
		if (state == MESSAGE_STATE_WAITING) {
			future.cancel();
			this.setState(MESSAGE_STATE_CANCELED);
		} else if (state == MESSAGE_STATE_INQUEUE) {
			this.setState(MESSAGE_STATE_CANCELED);
		}
	}

	public long getCreateTimestamp() {
		return createTimestamp;
	}

//	@Override
//	public boolean read() throws MessageParseException {
//		throw new UnsupportedOperationException();
//	}
//
//	@Override
//	public boolean write() throws MessageParseException {
//		throw new UnsupportedOperationException();
//	}
//
//	@Override
//	public void setBuffer(ByteBuffer buf) {
//		throw new UnsupportedOperationException();
//	}

	@Override
	public InternalSender getSender() {
		return sender;
	}

	@Override
	public void setSender(InternalSender sender) {
	}

	public String toString() {
		return StringUtils.obj2String(this, null);
	}
}
